import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_app_05/action/states/dark_states.dart';
import 'package:flutter_app_05/action/states/data_manager.dart';
import 'package:flutter_app_05/action/states/global.dart';
import 'package:flutter_app_05/utils/MyTheme.dart';
import 'package:flutter_app_05/widget/switch.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:provider/provider.dart';
import 'package:flutter_app_05/widget/sdk_extends.dart';

class Padecalseven extends StatefulWidget {
  @override
  _PadecalsevenInState createState() => _PadecalsevenInState();
}

class _PadecalsevenInState extends State<Padecalseven> {
  String hs = "<p/>";
  int page = 0;
  PageController mPageController = PageController(initialPage: 0);

  TextEditingController areaCon = new TextEditingController();
  TextEditingController hightCon = new TextEditingController();
  TextEditingController coefficientCon = new TextEditingController();
  TextEditingController flowCon = new TextEditingController();

  TextEditingController area2Con = new TextEditingController();
  TextEditingController hight2Con = new TextEditingController();
  TextEditingController flow2Con = new TextEditingController();

  TextEditingController coefficient2Con = new TextEditingController();

  Widget getpage() {
    return Container(
      height: 100,
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              Text(
                "*",
                style: TextStyle(color: Colors.red),
              ),
              Expanded(
                  child: Text(
                "供暖面积",
                style:
                    TextStyle(color: MyTheme(context).getColor('colorBlack')),
              )),
              Container(
                width: 100.0,
                child: TextField(
                  textAlign: TextAlign.right,
                  style: TextStyle(color: Colors.green),
                  maxLength: 100,
                  controller: areaCon,
                  decoration: null,
                  inputFormatters: [WhitelistingTextInputFormatter.digitsOnly],
                ),
              ),
              Text(
                "m²",
                style:
                    TextStyle(color: MyTheme(context).getColor('colorBlack')),
              ),
            ],
          ),
          Container(
            padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
            child: Divider(
              height: 1.0,
              indent: 0.0,
              color: MyTheme(context).getColor("line_color"),
            ),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              Expanded(
                  child: Text(
                "  预供流量",
                style:
                    TextStyle(color: MyTheme(context).getColor('colorBlack')),
              )),
              Container(
                width: 100.0,
                child: TextField(
                  textAlign: TextAlign.right,
                  style:
                      TextStyle(color: MyTheme(context).getColor('colorBlack')),
                  maxLength: 100,
                  decoration: null,
                  controller: flowCon,
                  inputFormatters: [WhitelistingTextInputFormatter.digitsOnly],
                ),
              ),
              Text(
                "L/m²",
                style:
                    TextStyle(color: MyTheme(context).getColor('colorBlack')),
              ),
            ],
          ),
          Container(
            padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
            child: Divider(
              height: 1.0,
              indent: 0.0,
              color: MyTheme(context).getColor("line_color"),
            ),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              Text(
                "  ",
                style: TextStyle(color: Colors.red),
              ),
              Expanded(
                  child: Text(
                "站内阻力",
                style:
                    TextStyle(color: MyTheme(context).getColor('colorBlack')),
              )),
              Container(
                width: 100.0,
                child: TextField(
                  textAlign: TextAlign.right,
                  style:
                      TextStyle(color: MyTheme(context).getColor('colorBlack')),
                  maxLength: 100,
                  controller: coefficientCon,
                  decoration: null,
                ),
              ),
              Text(
                "m",
                style:
                    TextStyle(color: MyTheme(context).getColor('colorBlack')),
              ),
            ],
          ),
          Container(
            padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
            child: Divider(
              height: 1.0,
              indent: 0.0,
              color: MyTheme(context).getColor("line_color"),
            ),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              Text(
                "*",
                style: TextStyle(color: Colors.red),
              ),
              Expanded(
                  child: Text(
                "阻力",
                style:
                    TextStyle(color: MyTheme(context).getColor('colorBlack')),
              )),
              Container(
                width: 100.0,
                child: TextField(
                  textAlign: TextAlign.right,
                  style:
                      TextStyle(color: MyTheme(context).getColor('colorBlack')),
                  maxLength: 100,
                  controller: hightCon,
                  decoration: null,
                ),
              ),
              Text(
                "m",
                style:
                    TextStyle(color: MyTheme(context).getColor('colorBlack')),
              ),
            ],
          ),
          Container(
            padding: EdgeInsets.fromLTRB(0, 10, 0, 0),
            child: Divider(
              height: 1.0,
              indent: 0.0,
              color: MyTheme(context).getColor("line_color"),
            ),
          ),
        ],
      ),
    );
  }

  Widget getpage2() {
    return Container(
      alignment: Alignment.center,
      height: 200,
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              Text(
                "*",
                style: TextStyle(color: Colors.red),
              ),
              Expanded(
                  child: Text(
                "供暖面积",
                style:
                    TextStyle(color: MyTheme(context).getColor('colorBlack')),
              )),
              Container(
                width: 100.0,
                child: TextField(
                  textAlign: TextAlign.right,
                  style: TextStyle(color: Colors.green),
                  maxLength: 100,
                  controller: area2Con,
                  decoration: null,
                  inputFormatters: [WhitelistingTextInputFormatter.digitsOnly],
                ),
              ),
              Text(
                "m²",
                style:
                    TextStyle(color: MyTheme(context).getColor('colorBlack')),
              ),
            ],
          ),
          Container(
            padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
            child: Divider(
              height: 1.0,
              indent: 0.0,
              color: MyTheme(context).getColor("line_color"),
            ),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              Expanded(
                  child: Text(
                "  预供流量",
                style:
                    TextStyle(color: MyTheme(context).getColor('colorBlack')),
              )),
              Container(
                width: 100.0,
                child: TextField(
                  textAlign: TextAlign.right,
                  style:
                      TextStyle(color: MyTheme(context).getColor('colorBlack')),
                  maxLength: 100,
                  controller: flow2Con,
                  decoration: null,
                  inputFormatters: [WhitelistingTextInputFormatter.digitsOnly],
                ),
              ),
              Text(
                "L/m²",
                style:
                    TextStyle(color: MyTheme(context).getColor('colorBlack')),
              ),
            ],
          ),
          Container(
            padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
            child: Divider(
              height: 1.0,
              indent: 0.0,
              color: MyTheme(context).getColor("line_color"),
            ),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              Text(
                "  ",
                style: TextStyle(color: Colors.red),
              ),
              Expanded(
                  child: Text(
                "站内阻力",
                style:
                    TextStyle(color: MyTheme(context).getColor('colorBlack')),
              )),
              Container(
                width: 100.0,
                child: TextField(
                  textAlign: TextAlign.right,
                  style:
                      TextStyle(color: MyTheme(context).getColor('colorBlack')),
                  maxLength: 100,
                  controller: coefficient2Con,
                  decoration: null,
                ),
              ),
              Text(
                "m",
                style:
                    TextStyle(color: MyTheme(context).getColor('colorBlack')),
              ),
            ],
          ),
          Container(
            padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
            child: Divider(
              height: 1.0,
              indent: 0.0,
              color: MyTheme(context).getColor("line_color"),
            ),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              Text(
                "*",
                style: TextStyle(color: Colors.red),
              ),
              Expanded(
                  child: Text(
                "高度",
                style:
                    TextStyle(color: MyTheme(context).getColor('colorBlack')),
              )),
              Container(
                width: 100.0,
                child: TextField(
                  textAlign: TextAlign.right,
                  style:
                      TextStyle(color: MyTheme(context).getColor('colorBlack')),
                  maxLength: 100,
                  controller: hight2Con,
                  decoration: null,
                ),
              ),
              Text(
                "m",
                style:
                    TextStyle(color: MyTheme(context).getColor('colorBlack')),
              ),
            ],
          ),
          Container(
            padding: EdgeInsets.fromLTRB(0, 10, 0, 0),
            child: Divider(
              height: 1.0,
              indent: 0.0,
              color: MyTheme(context).getColor("line_color"),
            ),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<DartState>(builder: (context, categoryState, child) {
      return columns(categoryState);
    });
  }

  Widget columns(DartState categoryState) {
    return Container(
      color: MyTheme(context).getColor("bg"),
      child: Column(
        children: [
          Container(
            margin: EdgeInsets.all(10.0),
            padding: EdgeInsets.all(15),
            decoration: new BoxDecoration(
                color: MyTheme(context).getColor("white"),
                borderRadius: new BorderRadius.circular((10.0))),
            alignment: Alignment.center,
            child: Column(
              children: [
                Container(
                  alignment: Alignment.center,
                  // width: 220,
                  height: 230,
                  child: PageView.builder(
                    itemCount: 2,
                    controller: mPageController,
                    onPageChanged: (index) {
                      print(index);
                      page = index;
                      setState(() {});
                    },
                    itemBuilder: (BuildContext context, int index) {
                      print(index);
                      return index == 0 ? getpage() : getpage2();
                    },
                  ),
                ),
                SwitchButton(
                  width: 280,
                  height: 40,
                  controller: mPageController,
                  tabs: ['循环泵', '补水泵'],
                  selectColor: Colors.green,
                  unSelectColor:Colors.white30,
                  currentIndex: mPageController.hasClients
                      ? mPageController.page?.round()
                      : mPageController.initialPage,
                ),
              ],
            ),
          ),
          Center(
            child: Html(data: hs),
          ),
          Expanded(
              child: Stack(
            children: [
              Positioned(
                child: Container(
                  height: 60.0,
                  alignment: Alignment.bottomCenter,
                  child: Row(
                    children: [
                      Expanded(
                        child: GestureDetector(
                          child: Container(
                            color: MyTheme(context).getColor("white"),
                            child: Center(
                              child: Text(
                                "重置",
                                style: new TextStyle(
                                    fontSize: 25.0,
                                    color: MyTheme(context)
                                        .getColor('text_color')),
                              ),
                            ),
                          ),
                          onTap: () {
                            if (page == 0) {
                              areaCon.text = "";
                              hightCon.text = "";
                              coefficientCon.text = "";
                              flowCon.text = "";
                            } else {
                              area2Con.text = "";
                              hight2Con.text = "";
                              flow2Con.text = "";
                              coefficient2Con.text = "";
                            }
                          },
                        ),
                      ),
                      Expanded(
                        child: GestureDetector(
                          child: Container(
                            color: Colors.green,
                            child: Center(
                              child: Text(
                                "提交",
                                style: new TextStyle(
                                    fontSize: 25.0, color: Colors.white),
                              ),
                            ),
                          ),
                          onTap: () {
                            if (page == 0) {
                              initInfo();
                            } else {
                              initInfo2();
                            }

                            setState(() {});
                          },
                        ),
                      )
                    ],
                  ),
                ),
                bottom: 0,
                left: 0,
                right: 0,
              ),
            ],
          )),
        ],
      ),
    );
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    // DataManager.getSbxx(p:);
  }

  void initInfo() async {
    await DataManager.getSbxx(p: {
      "size": "1",
      "lift": areaCon.text,
      "flow": flowCon.text,
      "hight": hightCon.text,
      "coefficient": coefficientCon.text,
      "from": "IOS"
    }).then((value) {
      hs = value.data;
      setState(() {});
    });
  }

  void initInfo2() async {
    await DataManager.getSbxx(p: {
      "size": "0",
      "lift": area2Con.text,
      "flow": flow2Con.text,
      "hight": hight2Con.text,
      "coefficient": coefficient2Con.text,
      "from": "IOS"
    }).then((value) {
      hs = value.data;
      setState(() {});
    });
  }
}
